<?php

include_once("class.phpmailer.php");
include_once("class.smtp.php");


function send_mail($to_arr, $subject, $content,$log_type=null)
{
    include(dirname(__FILE__) . '/config.php');
	
	$mail = new PHPMailer();
    $mail->CharSet = "UTF-8";
    $mail->IsSMTP(); // set mailer to use SMTP

    $mail->Host = $email_host; // specify main and backup server
    $mail->Port = $email_port; // set the port to use
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->SMTPSecure = 'ssl';
    
    $mail->Username = $email_user; // your SMTP username or your gmail username
    $mail->Password = $email_pass; // your SMTP password or your gmail password
    $from = $email_user; // Reply to this email
   
    $mail->From = $email_user;
    $mail->FromName = $email_name; // Name to indicate where the email came from when the recepient received
    
	foreach($to_arr as $to){
		$mail->AddAddress($to);
		 $mail->WordWrap = 50; // set word wrap
    $mail->IsHTML(true); // send as HTML
    $mail->Subject = $subject;
    $mail->Body = $content; //HTML Body
    
	//$mail->SMTPDebug = 2;
    if (!$mail->Send()) {
    
        //die("<h1>Loi khi goi mail: " . $mail->ErrorInfo . '</h1>');
        
		log_mail(null,$to, 0,time(),$subject.'</br>'.$content,1);
    } else {
        //echo 'sent to ' . $to;
        log_mail(null,$to, 1,time(),$subject.'</br>'.$content,1);
    }
	}
	
    
   
}
function get_latest_email_log($contact_id=null,$email){
	$logs=get_email_log($contact_id=null,$email);
	
	return $logs[0];
}
function get_email_log($contact_id=null,$email){
	require('config.php');
	$con=mysqli_connect($host,$user,$pass,$db);
	mysqli_set_charset($con,"utf8");
	// Check connection
	if (mysqli_connect_errno()) {
	  die ("Failed to connect to MySQL: " . mysqli_connect_error());
	}
	$email = mysql_real_escape_string($email);
	$sql="SELECT * FROM email_logs where mail_to='".$email."' order by date_sent desc";

	mysqli_query($con,"SET NAMES 'utf8'");	
	$result = mysqli_query($con,$sql);
	if(!$result){
		die(mysqli_error($con).':'.$sql);
	}
	
	$logs=array();
	while($row = mysqli_fetch_array($result)) {
	$logs[]=$row;    
	}
	mysqli_close($con);
	return $logs;
	
}

function log_mail($contact_id=null,$mail_to, $status,$date_sent,$description,$log_type)
{
    require('config.php');
	
	$con=mysqli_connect($host,$user,$pass,$db);
	mysqli_set_charset($con,"utf8");
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
	$description = mysql_real_escape_string($description);
	$mail_to = mysql_real_escape_string($mail_to);
	
    $sql_insert = "INSERT INTO `email_logs` (`contact_id`,`mail_to`, `date_sent`,`sent_status`,`description`,`log_type`) 
	VALUES ('" . $contact_id . "','" . $mail_to . "', '" . $date_sent . "','" . $status . "','" . $description . "','".$log_type."'); ";

	mysqli_query($con,"SET NAMES 'utf8'");	
    mysqli_query($con, $sql_insert);
    mysqli_close($con);
}





?>
